﻿using System;
using System.Collections.Generic;
using System.Text;
using Young3.FMSearch.Core.Entities.GameVersions;
using Young3.FMSearch.Core.Managers;
using Young3.FMSearch.Core.Offsets;
using System.Linq;
using Young3.FMSearch.Core.Attributes;

namespace Young3.FMSearch.Core.Entities.InGame
{
    public class Team : BaseObject
    {
        public Team(int memoryAddress, IVersion version)
            : base(memoryAddress, version)
        { }
                public Team(int memoryAddress, ArraySegment<byte> originalBytes, IVersion version)
            : base(memoryAddress, originalBytes, version)
        { }

        [FMEntityAttribute(TeamOffsets.NumberOfPlayers)]
        public Int32 NumberOfPlayers { get; set; }

        [FMEntityAttribute(TeamOffsets.Reputation)]
        public Int16 Reputation { get; set; }

        [FMEntityAttribute(TeamOffsets.Id)]
        public Int32 ID { get; internal set; }

        [FMPointer(TeamOffsets.ClubAddress)]
        public Club Club { get; internal set; }

        [FMPointer(TeamOffsets.LeagueAddress)]
        public League League { get; internal set; }

        [FMPointer(TeamOffsets.StadiumAddress)]
        public Stadium Stadium { get;  internal set; }

        [FMPointer(TeamOffsets.ManagerAddress)]
        public Staff Manager { get; internal set; }

        public TeamTypeEnum Type
        {
            get
            {
                switch (TeamType + TeamType2)
                {
                    case 100: return TeamTypeEnum.First;
                    case 256: return TeamTypeEnum.Empty;
                    case 255: return TeamTypeEnum.Amateur;
                    case 18: return TeamTypeEnum.U18s;
                    case 19: return TeamTypeEnum.U19s;
                    case 20: return TeamTypeEnum.U19s;
                    case 21: return TeamTypeEnum.U20s;
                    case 22: return TeamTypeEnum.U21s;
                    case 23: return TeamTypeEnum.B;
                    case 24: return TeamTypeEnum.U23s;
                    case 101: return TeamTypeEnum.Reserves;
                    default: return TeamTypeEnum.Empty;
                }
            }
        }

        [FMEntity(TeamOffsets.Type)]
        private byte TeamType { get; set; }

        [FMEntity(TeamOffsets.Type + 0x3)]
        private byte TeamType2 { get; set; }

        public override string ToString()
        {
            return (Club != null ? Club.ToString() : "") + " (" + Enum.GetName(typeof(TeamTypeEnum), Type) + ")";
        }
    }

    public enum TeamTypeEnum
    {
        First, 
        Empty, 
        Amateur,
        U18s,
        U19s,
        U20s,
        U21s,
        B,
        U23s,
        Reserves
    }
}
